#include "head.h"

int logon(const char *userName, const char *userPwd){
    struct spwd userInfo;
    struct spwd *result;
    char buf[1024] = {0};
    getspnam_r(userName,&userInfo,buf,sizeof(buf),&result);// 由用户名得到密文密码
    //ERROR_CHECK(result,NULL,"getspnam_r");
    struct crypt_data userData;
    bzero(&userData,sizeof(struct crypt_data));
    char *p = userInfo.sp_pwdp;
    size_t idx = 0;
    for(size_t i = 0; i < strlen(p); i++){
        if(p[i] == '$'){
            idx = i;
        }
    }
    char salt[1024] = {0};
    for(size_t i = 0; i <= idx; i++){
        salt[i] = p[i];// 获取盐值
    }
    salt[idx + 1] = '\0';
    crypt_r(userPwd,salt,&userData);// 根据明文密码和盐值得到密文密码
    int ret = strcmp(userInfo.sp_pwdp,userData.output);// 比较由用户明文密码得到的密文和服务端本地存储的密文
    return ret;
}

//int main(){
//    logon("playba","123");
//    return 0;
//}
